package com.epalmpay.controller.channel.mall;

import java.util.List;

import javax.annotation.Resource;

import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.mall.MainAdvertInDTO;
import com.epalmpay.dto.mall.MainAdvertListOutDTO;
import com.epalmpay.dto.mall.MainAdvertOutDTO;
import com.epalmpay.entity.MainAdvert;
import com.epalmpay.service.mall.IMainAdvertService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.PropertyCopyUtil;

/** 
 * 说明：首页轮播图
 * 创建人：gwn
 * 创建时间：2017-05-9
 */
@Controller
@RequestMapping(value="/merchant/mall")
public class MainAdvertController  extends BaseController{
	
	@Resource
	private IMainAdvertService mainAdvertService;
	@Resource
	private ISysLogService sysLogService;
	
	@RequestMapping(value="/mainAdvertList")
	@ResponseBody
	public MainAdvertListOutDTO  listMainAdverts(MainAdvertInDTO mainAdvertOut, PageDTO pageDTO){
		
		MainAdvertListOutDTO mainAdvertListOutDTO=new MainAdvertListOutDTO();
		try {
			Page page= Tools.pageEmtity(pageDTO);
			
			List<MainAdvertOutDTO> mainAdvertOutList=mainAdvertService.listMainAdverts(mainAdvertOut,page);
			mainAdvertListOutDTO.setMainAdvertOutList(mainAdvertOutList);

			mainAdvertListOutDTO.setPage(page);
			mainAdvertListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return mainAdvertListOutDTO;
			
		} 
		catch (ApplicationException ae) { 
			mainAdvertListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 mainAdvertListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return mainAdvertListOutDTO;
	}		

	
	@RequestMapping(value="/goAddMainAdvert")
	@ResponseBody
	public MainAdvertListOutDTO  goAddMainAdvert(MainAdvertInDTO mainAdvertOut){
		
		MainAdvertListOutDTO mainAdvertListOutDTO=new MainAdvertListOutDTO();
		try {
			mainAdvertListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return mainAdvertListOutDTO;
		}

		 catch (Exception e) { 
			 mainAdvertListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return mainAdvertListOutDTO;
	}		
	

	/**
	 * 新增时候保存首页轮播图
	 */
	@RequestMapping(value="/saveMainAdvert")
	@ResponseBody
	public MainAdvertListOutDTO  saveMainAdvert(MainAdvertInDTO mainAdvertInDTO){
		MainAdvertListOutDTO mainAdvertListOutDTO = new MainAdvertListOutDTO();
		try { 
            //保存首页轮播图
			mainAdvertService.saveMainAdvert(mainAdvertInDTO);
			mainAdvertListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			
			String content ="新增首页轮播图";
			sysLogService.addSysLog(mainAdvertInDTO.getUserId(),mainAdvertInDTO.getUserName(),mainAdvertInDTO.getIp(),content,mainAdvertInDTO.getGroupId());
			return mainAdvertListOutDTO;
		} catch (ApplicationException ae) { 
			mainAdvertListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			 mainAdvertListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return mainAdvertListOutDTO;
	}		
	
	
	/**
	 *编辑时  根据 id查询首页轮播图信息 
	 * 
	 */
	@RequestMapping(value="/goEditMainAdvert")
	@ResponseBody
	public MainAdvertOutDTO  findMainAdvertById(MainAdvertInDTO mainAdvertOut){
		MainAdvertOutDTO mainAdvertOuts = new MainAdvertOutDTO();
		MainAdvert mainAdvert=new MainAdvert();
		try {   
			mainAdvert.setId(mainAdvertOut.getId());
			MainAdvert mainAdverts=mainAdvertService.findMainAdvertById(mainAdvert);
			if(mainAdverts!=null){
				PropertyCopyUtil.copyPropertiesExclude(mainAdverts,mainAdvertOuts);
				mainAdvertOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return mainAdvertOuts;
			}else{
				mainAdvertOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			mainAdvertOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		mainAdvertOuts.setErrorCode("-2", "处理失败");
		return mainAdvertOuts;
	}
	
	
	/**
	 * 修改首页轮播图信息
	 */
	@RequestMapping(value="/updateMainAdvert")
	@ResponseBody
	public MainAdvertOutDTO  updateMemberGrade(MainAdvertInDTO mainAdvertOut){
		MainAdvertOutDTO mainAdvertOuts=new MainAdvertOutDTO();
		MainAdvert mainAdvert = new MainAdvert();
		try {   
			mainAdvert.setId(mainAdvertOut.getId());
			mainAdvert.setAdvertName(mainAdvertOut.getAdvertName());
			if(mainAdvertOut.getClickUrl()==null || mainAdvertOut.getClickUrl().equals("")){
				mainAdvert.setClickUrl("http://");
			}else{
				mainAdvert.setClickUrl(mainAdvertOut.getClickUrl());
			}
			mainAdvert.setPhotoUrl(mainAdvertOut.getPhotoUrl());
			mainAdvert.setSortNum(mainAdvertOut.getSortNum());
			mainAdvert.setStatus(mainAdvertOut.getStatus());
			
			
			mainAdvertService.editMainAdvert(mainAdvert);
			mainAdvertOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			String content ="修改首页轮播图";
			sysLogService.addSysLog(mainAdvertOut.getUserId(),mainAdvertOut.getUserName(),mainAdvertOut.getIp(),content,mainAdvertOut.getGroupId());
			
			return mainAdvertOuts;
		} catch (Exception e) { 
			mainAdvertOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		mainAdvertOuts.setErrorCode("-2", "处理失败");
		return null;
	}
	

	/**
	 * 删除首页轮播图
	 */
	@RequestMapping(value="/deleteMainAdvert")
	@ResponseBody
	public MainAdvertOutDTO  deleteMainAdvert(MainAdvertInDTO mainAdvertInDTO){
		
		MainAdvertOutDTO mainAdvertOutDTO=new MainAdvertOutDTO();
		MainAdvert mainAdvert = new MainAdvert();
		try {  
			mainAdvert.setId(mainAdvertInDTO.getId());
			mainAdvertService.deleteMemberGrade(mainAdvert); 
			mainAdvertOutDTO.setErrorCode(Rescode.SUCCESS, "删除首页轮播图信息成功");
			
			String content ="删除首页轮播图";
			sysLogService.addSysLog(mainAdvertInDTO.getUserId(),mainAdvertInDTO.getUserName(),mainAdvertInDTO.getIp(),content,mainAdvertInDTO.getGroupId());
			
			return mainAdvertOutDTO;
		} catch (Exception e) { 
			mainAdvertOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改状态
	 * @param mainAdvertInDTO
	 * @return
	 */
	@RequestMapping(value="/updateMainAdvertStatus")
	@ResponseBody
	public MainAdvertOutDTO  updateMainAdvertStatus(MainAdvertInDTO mainAdvertInDTO){
		MainAdvertOutDTO mainAdvertOutDTO=new MainAdvertOutDTO();
//		MainAdvert mainAdvert = new MainAdvert();
		try {  
			MainAdvert mainAdverts=mainAdvertService.findMainAdvert(mainAdvertInDTO.getId());
			mainAdvertService.updateStatus(mainAdverts);
			
			mainAdvertOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			String content ="修改首页轮播图状态";
			sysLogService.addSysLog(mainAdvertInDTO.getUserId(),mainAdvertInDTO.getUserName(),null,content,mainAdvertInDTO.getGroupId());
			
			return mainAdvertOutDTO;
		} catch (Exception e) { 
			mainAdvertOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}

}
